package com.mid;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by Lxk on 2020/1/31.
 */
public class Solution131 {

    private List<List<String>> res = new ArrayList<>();

    private Stack<String> stack = new Stack<>();

    private void findFitStr(String base, int pos, StringBuffer str) {
        if (pos >= base.length()) {
            if (isFit(str.toString())) {
                stack.push(str.toString());
                StringBuffer appendAll = new StringBuffer();
                for (String s : stack) {
                    appendAll.append(s);
                }
                if (appendAll.toString().equals(base)) {
                    List<String> curList = new ArrayList<>();
                    for (String s : stack) {
                        curList.add(s);
                    }
                    res.add(curList);
                }
                stack.pop();
            }
            return;
        }
        StringBuffer strForAppend = new StringBuffer(str.toString());
        StringBuffer strForAdd = new StringBuffer(str.toString());
        //1、作为新字符串开端，旧字符串入栈;
        if (isFit(strForAdd.toString())) {
            stack.push(strForAdd.toString());
            strForAdd = new StringBuffer();
            strForAdd.append(base.charAt(pos));
            findFitStr(base, pos + 1, strForAdd);
            stack.pop();
        }
        //2、加入到当前字符串；
        strForAppend.append(base.charAt(pos));
        findFitStr(base, pos + 1, strForAppend);
    }

    private boolean isFit(String str) {
        if (str == null || str.length() <= 0) {
            return false;
        }
        for (int i = 0; i <= str.length() / 2; i++) {
            if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
                return false;
            }
        }
        return true;
    }

    public List<List<String>> partition(String s) {
        StringBuffer sb = new StringBuffer();
        findFitStr(s, 0, sb);
        return res;
    }

    public static void main(String[] args) {
        Solution131 solution131 = new Solution131();
        System.out.println(solution131.partition("aab"));
    }

}
